<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Catalog extends Controller_Index_Base
{
    protected $_is_regional = true;
    
	public function action_index()
	{ 
        $category = $this->request->param('category') ? $this->request->param('category') : null;
        $tag      = $this->request->param('tag') ? $this->request->param('tag') : null;
        $page     = $this->request->param('page') ? $this->request->param('page') : 1;
         
        $from_price = (isset($_GET['from_price']) && $_GET['from_price']) ? $_GET['from_price'] : null;
        $to_price   = (isset($_GET['to_price']) && $_GET['to_price']) ? $_GET['to_price'] : null; 
        $brand      = (isset($_GET['brand']) && $_GET['brand']) ? $_GET['brand'] : null;  
        $shop       = (isset($_GET['shop']) && $_GET['shop']) ? $_GET['shop'] : null;
        
        if ($category) {
            $category = ORM::factory('category')->get_category_by_url($category);   
        }
        
        if ($category && !$category->is_bigcat) { 
            if ($tag) {
                $tag = ORM::factory('tag')->get_tag_by_url($category, $tag);    
            }
                    
            $categories   = ORM::factory('category')->get_categories($category);   
            $brands       = ORM::factory('brand')->get_brands($category);        
            $prices       = Model_Product::get_prices();
            $shops        = ORM::factory('shop')->get_shops($category);
            $products     = ORM::factory('product')->get_products($category, $tag, $brand, $shop, array('from' => $from_price, 'to' => $to_price), $page);
               
            $content = new View('index/catalog/index_tpl'); 
            $content->category = $category;
            $content->tag      = $tag;
            $content->breadcrumbs = null;
            if ($category) {
                $content->breadcrumbs = $category->get_breadcrumbs();   
            }
            $content->categories  = $categories;
            $content->brands   = $brands;
            $content->shops    = $shops;
            $content->prices   = $prices;
            $content->products = $products;
            $content->coupons  = ORM::factory('coupon')->get_coupons_by_products($products);
            $_shops   = ORM::factory('shop')->get_shops_by_products($products);
            $content->_shops   = $_shops;
            //$content->products_data = ORM::factory('product')->get_products_data($products);
            $content->tags     = ORM::factory('tag')->get_tags_by_category($category, $tag);
            $content->videoreviews = ORM::factory('shop')->get_videoreviews_by_shops($_shops);
            
            $count = ORM::factory('product')->get_products($category, $tag, $brand, $shop, array('from' => $from_price, 'to' => $to_price), null, true);    
            $content->pagination = array(                                                   
                'page' => $page,
                'per_page' => Model_Product::get_products_per_page(),
                'count' => $count,
            );
            
            $brand_filter = array();
            if ($brand) {
                $brand_filter = explode(',', $brand);    
            }
            $shop_filter = array();
            if ($shop) {
                $shop_filter = explode(',', $shop);    
            }
            $content->filters = array(
                'from_price' => $from_price,
                'to_price'   => $to_price,
                'brand'      => $brand_filter,
                'shop'       => $shop_filter,
            ); 
            
            $title       = Helper_Index::get_catalog_index_title($category, $tag, $page); 
            $description = Helper_Index::get_catalog_index_description($category, $tag, $page);  
        } else {
            $content = new View('index/catalog/bigcat_tpl');
            $content->category = $category;
            $content->breadcrumbs = $category->get_breadcrumbs(); 
            $content->category_tree = $category->get_category_tree();
            $title       = Helper_Index::get_catalog_index_title($category); 
            $description = Helper_Index::get_catalog_index_description($category);      
        }      
        $this->print_template($title, $content, $description);
	}
    
    public function action_redir()
    {
        header('Location: /'); 
        die();  
    }
    
    public function action_filter_count_ajax()
    {
        $result = array(
            'success'  => false,
        );
        if ($this->request->is_ajax()) {
            $category_id = $this->request->post('category_id');
            $tag_id      = $this->request->post('tag_id');
            $from_price  = $this->request->post('from_price'); 
            $to_price    = $this->request->post('to_price');
            $brand       = $this->request->post('brand');
            $shop        = $this->request->post('shop');
            
            $count  = ORM::factory('product')->get_products($category_id, $tag_id, $brand, $shop, array('from' => $from_price, 'to' => $to_price), null, true);
            $count_form = $count.' '.Helper_Index::plural_form($count, array('товар', 'товара', 'товаров'));             
            $result = array(
                'success' => true,
                'count'   => $count,
                'count_form' => $count_form, 
            );
        }
        //header('Content-type: text/json');
        echo json_encode($result);
        die();
    }
} 
